﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "WallParametersCustomizer.h"
#include "IParameter.h"
#include "DbObjectUtils.h"
#include "IParameterDefinitionLibrary.h"
#include "ParameterDefine.h"

#include "Wall.h"
#include "WallData.h"
#include "IGenericElement.h"
#include "ILine3d.h"
#include "IParameterDefinition.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

DBOBJECT_DATA_DEFINE(WallParametersCustomizer)
{
    SetOwnerElement(nullptr);
}

gcmp::OwnerPtr<gcmp::IParameter> Sample::WallParametersCustomizer::GetNativeParameter(int parameterId) const
{
    const IGenericElement *pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    const Wall *pWall = quick_cast<Wall>(pGenericElement->GetExternalObject());
    IDocument* pDoc = GetOwnerElement()->GetDocument();
    if (parameterId == GET_UIPLATFORM_PARAMETER_ID(WIDTH_PARAMETER))
    {
        ParameterAttributes parameterAttributes = { false, false, true };
        const IParameterDefinition* pDef = GetParamDef_WIDTH_PARAMETER();
        OwnerPtr<IParameter> opParameter = IParameter::CreateParameter(pDoc, pDef->GetStorageType(),
            parameterAttributes, pDef->GetUid(), ParameterProcessType::GeneralInput);
        opParameter->SetValueAsDouble(pWall->GetData()->GetWidth());
        return TransferOwnership(opParameter);
    }
    if (parameterId == GET_UIPLATFORM_PARAMETER_ID(HEIGHT_PARAMETER))
    {
        ParameterAttributes parameterAttributes = { false, false, true };
        const IParameterDefinition* pDef = GetParamDef_HEIGHT_PARAMETER();
        OwnerPtr<IParameter> opParameter = IParameter::CreateParameter(pDoc, pDef->GetStorageType(),
            parameterAttributes, pDef->GetUid(), ParameterProcessType::GeneralInput);
        opParameter->SetValueAsDouble(pWall->GetData()->GetHeight());
        return TransferOwnership(opParameter);
    }
    if (parameterId == GET_UIPLATFORM_PARAMETER_ID(LENGTH_PARAMETER))
    {
        ParameterAttributes parameterAttributes = { false, false, true };
        const IParameterDefinition* pDef = GetParamDef_LENGTH_PARAMETER();
        OwnerPtr<IParameter> opParameter = IParameter::CreateParameter(pDoc, pDef->GetStorageType(),
            parameterAttributes, pDef->GetUid(), ParameterProcessType::GeneralOutput);
        Vector3d dir = pWall->GetData()->GetStartPoint() - pWall->GetData()->GetEndPoint();
        opParameter->SetValueAsDouble(dir.Length());
        return TransferOwnership(opParameter);
    }
    return nullptr;
}

bool Sample::WallParametersCustomizer::SetNativeParameter(const IParameter *pParameter, std::wstring* errorMessage /*= nullptr*/)
{
    IGenericElement *pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    Wall* pWall = quick_cast<Wall>(pGenericElement->GetExternalObject());
    WallData* pData = pWall->GetData();
    if (pParameter->GetParameterDefinitionId() == GET_UIPLATFORM_PARAMETER_ID(WIDTH_PARAMETER))
    {
        pData->SetWidth(pParameter->GetValueAsDouble());
        pWall->MarkDataRdId();
        return true;
    }
    else if (pParameter->GetParameterDefinitionId() == GET_UIPLATFORM_PARAMETER_ID(HEIGHT_PARAMETER))
    {
        pData->SetHeight(pParameter->GetValueAsDouble());
        pWall->MarkDataRdId();
        return true;
    }
    else if (pParameter->GetParameterDefinitionId() == GET_UIPLATFORM_PARAMETER_ID(LENGTH_PARAMETER))
    {
        Vector3d dir = pData->GetStartPoint() - pData->GetEndPoint();
        dir.Normalize();
        Vector3d ptEnd = pData->GetStartPoint() + pParameter->GetValueAsDouble() * dir;
        pData->SetEnd(ptEnd);
        pWall->MarkDataRdId();
        return true;
    }
    return false;
}

void Sample::WallParametersCustomizer::ReportParameterDefinitions(std::vector<int>* parameterIds) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(parameterIds, L"parameterIds不能为空", L"GDMP", L"2023-09-30");
    parameterIds->push_back(GET_UIPLATFORM_PARAMETER_ID(WIDTH_PARAMETER));
    parameterIds->push_back(GET_UIPLATFORM_PARAMETER_ID(HEIGHT_PARAMETER));
    parameterIds->push_back(GET_UIPLATFORM_PARAMETER_ID(LENGTH_PARAMETER));

}

gcmp::IDocument* WallParametersCustomizer::GetDocument() const
{
    return GetOwnerElement()->GetDocument();
}

gcmp::NdbObject* WallParametersCustomizer::GetTopOwnerObject() const
{
    return quick_cast<gcmp::NdbObject>(m_pOwnerElement);
}

